<% include ../header.ejs %>
<script src="/marked/lib/marked.js"></script>
<div class="container post-show">
    <!--左边栏-->
    <div class="col-sm-12 col-md-9" id="left-panel" style="padding-left: 0; padding-right: 0">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div>
                    <div>
                        <span style="font-size: 30px; font-weight: bold" id="post-title"><%= post.title %></span>
                    </div>
                    <div>
                        <span style="color: purple;"><%= post.category %></span>
                        <span style="color: lightsalmon;"><%= post.tags && post.tags.join(', ') %></span>
                        <span style="color: #5e6e5e; float: right"><%= post.showHandleCreatedAt() %></span>
                    </div>
                </div>
            </div>
            <div class="panel-body" style="overflow: scroll">
                <div class="markdown">
                    <textarea class="markdownText" hidden><%= post.markdown %></textarea>
                </div>
            </div>
        </div>
        <!--显示评论-->
        <div class="panel panel-default">
            <div class="panel-heading">
                共有<%= comments.length %>条评论
            </div>
            <div class="panel-body">
                <% comments.forEach(function(comment){ %>
                <% include ../sumMondules/commentListItem.ejs %>
                <% }) %>
            </div>
        </div>
        <!--添加评论 -->
        <div class="panel panel-default">
            <div class="panel-body">
                <form class="form" id="comment-form">
                    <div class="col-md-12" style="padding-left: 0">
                        <div class="col-md-6" style="padding-left: 0; overflow: hidden">
                            <div class="form-group">
                                <input type="text" value="<%= post._id %>" id="comment-post-id" hidden>
                                <input type="text" value="<%= post.author._id %>" id="to-user-id" hidden>
                                <div class="controls">
                                    <textarea class="form-control" name="markdown" rows="5" id="comment-input" placeholder="请输入评论内容(支持markdown语法)"></textarea>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6" style="background-color: #eee; padding-top: 10px">
                            <div id="preview"></div>
                        </div>
                    </div>

                    <input type="submit" value="提交评论" class="btn btn-primary"/>
                </form>
            </div>
        </div>
    </div>

    <!--右边栏-->
    <div class="hidden-sm hidden-xs col-md-3">
        <ul class="list-group">
            <li class="list-group-item active">文章信息</li>
            <li class="list-group-item">
                <div class="list-group-heading" style="font-size: 12px">用户名</div>
                <div class="list-group-text"><%= post.author.username %></div>
            </li>
            <li class="list-group-item">
                <div class="list-group-heading" style="font-size: 12px">邮箱</div>
                <div class="list-group-text"><%= post.author.githubInfo.email %></div>
            </li>
            <li class="list-group-item">
                <div class="list-group-heading" style="font-size: 12px">阅读量</div>
                <div class="list-group-text"><%= post.browseCount %></div>
            </li>
        </ul>
        <% if (locals.user && post.author._id.toString() == locals.user.mongo_id){ %>
        <div class="panel panel-default">
            <div class="panel-heading">
                <div class="panel-title">操作</div>
            </div>
            <div class="panel-body">
                <div class="text-center">
                    <input id="post-id" type="text" value="<%= post.id %>" hidden>
                    <a href="/posts/edit/<%= post.id %>" class="btn btn-primary">编辑</a>
                    <button id="delete-post" class="btn btn-danger">删除</button>
                </div>
            </div>
        </div>
        <% } %>
    </div>
</div>

<% include ../footer.ejs %>

<link rel="stylesheet" href="/highlight/src/styles/tomorrow-night-eighties.css">
<script src="/javascripts/highlight.pack.js"></script>
<script>
    $(function(){
        $('#nav-posts').addClass('active');

        $('.markdown').each(function(){
            var markdown = $(this).find('.markdownText')[0].value;
            $(this).html(marked(markdown));
        });

        hljs.initHighlighting();

        //  删除帖子
        $('#delete-post').on('click', function(){
            if(!confirm('确定要删除该帖子吗?')) return ;
            $.ajax({
                url: '/posts/api/delete/' + $('#post-id').val(),
                type: 'DELETE',
                success: function(data){
                    location.href = '/posts';
                },
                error: function(data){
                    console.log(data);
                }

            })
        });

        // 评论
        $('#comment-input').on('input', function(){
            document.getElementById('preview').innerHTML = marked($(this).val());
            hljs.initHighlighting.called = false;
            hljs.initHighlighting();
        });

        var allowCommit = true;
        $('#comment-form').on('submit', function(e){
            e.preventDefault();
            if (allowCommit){
                allowCommit = false;
            }else {
                return ;
            }

            var markdown = $('#comment-input').val();
            if (!markdown){
                alert('评论内容不能为空');
                return ;
            }
            $.ajax({
                type: "POST",
                url: "/comments/api/new",
                data: {
                    toUserId: $('#to-user-id').val(),
                    postId: $('#comment-post-id').val(),
                    markdown: markdown,
                    postTitle: $('#post-title').text()
                },
                success: function(data){
                    if(!data.success){
                        alert(data.msg);
                        allowCommit = true;
                        return ;
                    }
                    location.reload();
                },
                error: function(){
                    allowCommit = true;
                    alert('评价失败');
                }
            });
        });
    });

</script>